package com.jie.servlet;

import com.alibaba.fastjson.JSON;
import com.jie.service.AgencyService;
import com.jie.service.impl.AgencyServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * Created by 12904 on 2018/8/1.
 */
public class indexServlet extends HttpServlet
{

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        doPost(request,response);
    }

    int page = 0;
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        response.setContentType("text/html;charset=utf-8");
        PrintWriter out = response.getWriter();
        request.setCharacterEncoding("utf-8");
        int size = 5;
        String flag =request.getParameter("flag");
        if (request.getParameter("flag") != null){
            page =page+ Integer.parseInt(request.getParameter("flag"));
        }else {
            page = 0;
        }
        if (page < 0){
            page = 0;
        }

        AgencyService service = new AgencyServiceImpl();
            int count = service.find();
            int maxPage=(count+size-1)/size;
            if( page > maxPage-1){
                page = maxPage-1;
        }
        String json = JSON.toJSONString(service.find(page * size ,size));
        out.print(json);
        out.flush();
        out.close();
    }
}
